﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TPAMain工序结余锁定工具
{
    public partial class frmLock : Form
    {
        int iLockMode = 0;   //锁定模式：1、指定工单；2、指定批号；3、指定芯片
        string strKey = string.Empty;
        string strOperator = string.Empty;
        DataTable dt;       //数据集
        SqlTools sqt_db;
        SqlTools sqt_tp;

        /// <param name="iTmpLockMode">锁定模式：1、指定工单；2、指定批号；3、指定芯片</param>
        /// <param name="strTmpKey"></param>
        /// <param name="tmpdt"></param>
        public frmLock(int iTmpLockMode,string strTmpOperator,string strTmpKey,DataTable tmpdt)
        {
            InitializeComponent();
            iLockMode = iTmpLockMode;
            strOperator = strTmpOperator;
            strKey = strTmpKey;
            string strLockMode = string.Empty;
            switch (iLockMode)
            {
                case 1:
                    strLockMode = "指定工单";
                    break;
                case 2:
                    strLockMode = "指定批号";
                    break;
                case 3:
                    strLockMode = "指定芯片";
                    break;
            }
            this.Text = strLockMode + " - " + strKey;
            dt = tmpdt;
            dgv1.DataSource = dt;
            dgv1.AutoResizeColumns();
            sqt_db = new SqlTools(GeneralTools.GetConnectionString("DefualtDataBase"));
            sqt_tp = new SqlTools(GeneralTools.GetConnectionString("TPAMain"));
        }

        private void btnOperate_Click(object sender, EventArgs e)
        {
            if (dgv1.Rows.Count == 0 ||(chkLockSelected .Checked &&dgv1 .SelectedRows .Count ==0))
            {
                GeneralTools.ShowInfoMsg("没有要处理的数据！");
                return;
            }
            if (txtReason.Text.Trim().Equals(string.Empty))
            {
                GeneralTools.ShowInfoMsg("锁定原因不能为空！");
                return;
            }
            string strLockReason = txtReason.Text.Trim();
            DialogResult dr = MessageBox.Show("确认锁定选定的记录吗？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.OK)
            {
                //创建锁定清单
                DataTable tmpdt = new DataTable();
                tmpdt.Columns.Add("工序结余ID");
                tmpdt.Columns.Add("原始工序结余");
                tmpdt.Columns.Add("原始工序结余备注");
                tmpdt.Columns.Add("工单号");
                tmpdt.Columns.Add("批号");
                tmpdt.Columns.Add("芯片号");
                for (int i=0;i<dgv1 .Rows.Count;i++)
                {
                    if (chkLockSelected .Checked &&dgv1.Rows [i].Selected ==false )
                    {
                        continue;
                    }
                    DataRow tmpdr = tmpdt.NewRow();
                    tmpdr["工序结余ID"] = dgv1.Rows[i].Cells["工序结余ID"].Value;
                    tmpdr["原始工序结余"] = dgv1.Rows[i].Cells["结余数量"].Value;
                    tmpdr["原始工序结余备注"] = dgv1.Rows[i].Cells["备注"].Value;
                    tmpdr["工单号"] = dgv1.Rows[i].Cells["工单号"].Value;
                    tmpdr["批号"] = dgv1.Rows[i].Cells["批号"].Value;
                    tmpdr["芯片号"] = dgv1.Rows[i].Cells["芯片号"].Value;
                    tmpdt.Rows.Add(tmpdr);
                }
                string strLockMode = string.Empty;
                switch (iLockMode)
                {
                    case 1:
                        strLockMode = "指定工单";
                        break;
                    case 2:
                        strLockMode = "指定批号";
                        break;
                    case 3:
                        strLockMode = "指定芯片";
                        break;
                }
                if (LockHelper.DoLock(strLockMode, strKey, strOperator, strLockReason, tmpdt))
                {
                    GeneralTools.ShowInfoMsg(tmpdt.Rows.Count.ToString() + "条记录锁定成功！");
                    this.Close();
                }
            }
        }
    }
}
